$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        staffList: null,
        depId: 0,
        sStaffName: null,
        userId: null,
        iTTId: 0,
        iType: 0,
        init: function () {
            this.funObj().initDepTree();
            this.iTTId = xhTool.getQuery('iTTId');
            this.iType = xhTool.getQuery('iType');
            this.staffList = new xhDataGrid($('#assignTask'), 'track/getStaff', {
                sStaffName: this.sStaffName,
                iDepId: this.depId
            });
            this.staffList.parseToolbar().init();
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#bt_list_search').click(function () {
                $this.sStaffName = $('#staffName').val();
                $this.staffList.reload({sStaffName: $this.sStaffName, iDepId: $this.depId});
            });
            $this.staffList.onSelect = function (rowIndex, rowData) {
                $this.userId = rowData.iUserId;
            };
            $this.staffList.onLoadSuccess = function () {
                $this.userId = 0;
            };
            $('#bt_modal_confirm', parent.document).click(function () {
                $this.funObj().submitForm();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                initDepTree: function () {
                    $('#depList').treegrid({
                        url: 'getDepartments',
                        method: 'post',
                        idField: 'iDepId',
                        treeField: 'sDepName',
                        singleSelect: true,
                        columns: [[
                            {field: 'ck', checkbox: true},
                            {field: 'iDepId', title: 'iDepId', hidden: true},
                            {field: 'sDepName', title: '名称', width: 260, align: 'left'},
                        ]],
                        onSelect: function (row) {
                            $this.depId = row['iDepId'];
                            $('#depList').treegrid('expand', $this.depId);
                            $this.staffList.reload({sStaffName: $this.sStaffName, iDepId: $this.depId});
                        },
                        onUnselect: function () {
                            $this.staffList.reload({sStaffName: 0, iDepId: 0});
                        },
                        onBeforeSelect: function (row) {
                            if ($this.depId == 0) {
                                return true;
                            } else {
                                if ($this.depId == row['iDepId']) {
                                    $('#depList').treegrid('unselect', $this.depId);
                                    $('#depList').treegrid('collapse', $this.depId);
                                    $this.depId = 0;
                                    return false;
                                }
                            }
                        },
                        onBeforeCheck: function (row) {
                            if ($this.depId == 0) {
                                return true;
                            } else {
                                if ($this.depId == row['iDepId']) {
                                    $('#depList').treegrid('unselect', $this.depId);
                                    $('#depList').treegrid('collapse', $this.depId);
                                    $this.depId = 0;
                                    return false;
                                }
                            }
                        },
                    });
                },
                submitForm: function () {
                    xhExtra.setBtnLoading($('#bt_modal_confirm', parent.document));
                    $('#bt_modal_confirm', parent.document).addClass('disabled');
                    var data = {
                        'userId': $this.userId,
                        'iTTId': $this.iTTId,
                        'iType': $this.iType,
                    }
                    window.top.swal({
                        title: '确认指派？',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '确认',
                        allowOutsideClick: true,
                        showCloseButton: true,
                        showLoaderOnConfirm: true,
                        type: 'warning',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                xhExtra.ajaxRequest('track/assign', data, {check:false,success:false}, function (res) {
                                    if(xhExtra.checkError(res)){
                                        xhExtra.setBtnNormal($('#bt_modal_confirm', parent.document));
                                        $('#bt_modal_confirm', parent.document).removeClass('disabled');
                                        return false;
                                        window.top.xhTop.showError(res.msg);
                                    }
                                    window.top.xhTop.showSuccess(res.msg);
                                    window.top.xhTop.getActiveFrame().list.dataGridReload();
                                    window.top.xhTop.closeModal();
                                });
                            })
                        }
                    }).then(function(isConfirm) {}, function(dismiss) {});
                }

            }
        }
    }
};
